package com.sumainfor.pc.market.service;

import com.sumainfor.common.util.DateUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.CompuUtils;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.dao.HouOwerMapper;
import com.sumainfor.pc.market.feign.TenantFeign;
import com.sumainfor.pc.market.feign.Zfeign.ZpcGmioFeign;
import com.sumainfor.pc.market.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.market.feign.ZuulFeign.ZuulFeign;
import org.apache.commons.collections.MapUtils;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Meng
 * @ClassName HouOwerServiceImpl
 * @date 2020/2/17 14:57
 * @description: TODO
 */

@Service
public class HouOwerServiceImpl {

    @Autowired
    HouOwerMapper houOwerMapper;

    @Autowired
    ZpcGmioFeign zpcGmioFeign;

    @Autowired
    ZzuulFeign zzuulFeign;

    @Autowired
    ZuulFeign zuulFeign;

    @Autowired
    TenantFeign tenantFeign;

    /**
     * 收房合同基础信息表
     * @param params
     * @param pager
     * @return
     */
    public JsonResult getHouOwer(Map<String,Object> params, Pager pager){
        JsonResult result = new JsonResult();


        pager.setPageno((Integer) params.get("pageno"));
        pager.setPagesize((Integer) params.get("pagesize"));
        pager.setPagerNecessary(params,pager);
        PageUtils pageUtils = new PageUtils();

        Integer cout = houOwerMapper.getHouOwerCount(params);
        if(cout == 0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(),params,cout);
        }else {
            List<Map<String, Object>> getHouOwer = houOwerMapper.getHouOwer(params);

            for (Map<String,Object> map1:getHouOwer){

                //服务中心
                String deptId = MapUtils.getString(map1,"MiId");
                Map<String,Object> fuwuMap =  zzuulFeign.getDeptInfoById(deptId);
                map1.putAll(fuwuMap);


                map1.put("area_id",map1.get("CorId"));

                //所属公司
                Map<String,Object> mapArea = zuulFeign.getArea(map1);
                System.out.println("11111111111111111111111111");
                System.out.println("mapArea"+mapArea);
                map1.putAll(mapArea);


                Integer OwerTypeCount = houOwerMapper.getOwerDealType(map1);


                //业主
                List<Map<String,Object>> ceshiOwerList = houOwerMapper.getHousOwner(map1);
                map1.put("ceshiOwerList",ceshiOwerList);




                if (OwerTypeCount==0){
                    map1.put("OwerDealType","新签");


                }else{
                    map1.put("OwerDealType","续签");
                }




                double oneYear = MapUtils.getDouble(map1,"MonthPrice");
                double twoYear = MapUtils.getDouble(map1,"MonthPrice");
                double threeYear =MapUtils.getDouble(map1,"MonthPrice");
                double fourYear = MapUtils.getDouble(map1,"MonthPrice");
                double fiveYear = MapUtils.getDouble(map1,"MonthPrice");
                double sixYear =MapUtils.getDouble(map1,"MonthPrice");
                double sevenYear = MapUtils.getDouble(map1,"MonthPrice");
                double eightYear = MapUtils.getDouble(map1,"MonthPrice");





                double xuzuoneYear = 0;
                double xuzutwoYear = 0;
                double xuzuthreeYear =0;
                double xuzufourYear = 0;
                double xuzufiveYear = 0;
                double xuzusixYear = 0;



                int AscendingYea = MapUtils.getInteger(map1,"AscendingYea");//租金递增年
                int AscendingType =MapUtils.getInteger(map1,"AscendingType");//租金递增类型   1.百分比   2.元
                double AscendingVal =MapUtils.getDouble(map1,"AscendingVal");//租金递增值

//                if (AscendingYea==1){
//                    if (AscendingType==1){
//
//                    }else{
//                        twoYear =  CompuUtils.add(oneYear,AscendingVal);
//                        threeYear =  CompuUtils.add(twoYear,AscendingVal);
//                        fourYear =  CompuUtils.add(threeYear,AscendingVal);
//                        fiveYear =  CompuUtils.add(fourYear,AscendingVal);
//                        sixYear =  CompuUtils.add(fiveYear,AscendingVal);
//                        sevenYear =  CompuUtils.add(sixYear,AscendingVal);
//                        eightYear =  CompuUtils.add(sevenYear,AscendingVal);
//                    }
//
//
//
//
//                }else if(AscendingYea==2){
//
//
//                    if (AscendingType==1){
//
//                    }else{
//                        twoYear= CompuUtils.add(oneYear,AscendingVal);
//                        threeYear =  CompuUtils.add(oneYear,AscendingVal);
//                        fourYear =  CompuUtils.add(twoYear,AscendingVal);
//                        fiveYear =  CompuUtils.add(twoYear,AscendingVal);
//                        sixYear =  CompuUtils.add(fiveYear,AscendingVal);
//                        sevenYear =  CompuUtils.add(fiveYear,AscendingVal);
//                        eightYear =  CompuUtils.add(sevenYear,AscendingVal);
//                    }
//
//
//                }else if(AscendingYea==3){
//
//                    if (AscendingType==1){
//
//                    }else{
//                        twoYear= CompuUtils.add(oneYear,AscendingVal);
//                        threeYear =  CompuUtils.add(oneYear,AscendingVal);
//                        fourYear =  CompuUtils.add(twoYear,AscendingVal);
//                        fiveYear =  CompuUtils.add(twoYear,AscendingVal);
//                        sixYear =  CompuUtils.add(fiveYear,AscendingVal);
//                        sevenYear =  CompuUtils.add(fiveYear,AscendingVal);
//                        eightYear =  CompuUtils.add(sevenYear,AscendingVal);
//                    }
//
//
//
//                }else if(AscendingYea==4){
//
//                }else if(AscendingYea==5){
//
//                }else if(AscendingYea==6){
//
//                }













                String xuzuMian ="";//续签免租期
                String xuqianStartime ="";//续签开始
                 String xuqianEndtime ="";//续签结束
                 String xuqiantime="";//续签日期




                if (MapUtils.getString(map1,"NextOwDeId")!=null&&!MapUtils.getString(map1,"NextOwDeId").equals("")){
                    Map<String,Object> zcMap = new HashMap<>();
                    zcMap.put("OwDeId",map1.get("NextOwDeId"));
                    List<Map<String,Object>> zcList = houOwerMapper.getHouOwer(zcMap);
                    String mianzuqi = MapUtils.getString(zcList.get(0),"StartTime");
                    int RentHoliday = MapUtils.getInteger(zcList.get(0),"RentHoliday");

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
                    Date date = new Date();
                    try {
                        date = simpleDateFormat.parse(mianzuqi);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    Date  mianzuqiDate =  DateUtils.addDateMonthsToEnd(date,RentHoliday);
                    xuzuMian =   simpleDateFormat.format(mianzuqiDate);
                    map1.put("xuzuMian",xuzuMian);//续租免
                    map1.put("xuqianStartime",MapUtils.getString(zcList.get(0),"StartTime"));
                    map1.put("xuqianEndtime",MapUtils.getString(zcList.get(0),"EndTime"));
                    map1.put("xuqiantime",MapUtils.getString(zcList.get(0),"Owerdealinstm"));



                     xuzuoneYear = MapUtils.getDouble(zcList.get(0),"MonthPrice");
                     xuzutwoYear = MapUtils.getDouble(zcList.get(0),"MonthPrice");
                     xuzuthreeYear =MapUtils.getDouble(zcList.get(0),"MonthPrice");
                     xuzufourYear = MapUtils.getDouble(zcList.get(0),"MonthPrice");
                     xuzufiveYear = MapUtils.getDouble(zcList.get(0),"MonthPrice");
                    xuzusixYear =MapUtils.getDouble(zcList.get(0),"MonthPrice");





                }else {
                    map1.put("xuzuMian",xuzuMian);//续租免
                    map1.put("xuqianStartime",xuqianStartime);
                    map1.put("xuqianEndtime",xuqianEndtime);
                    map1.put("xuqiantime",xuqiantime);
                }


                String TuiDay = "";
                if (MapUtils.getInteger(map1,"delfg")==0){
                    TuiDay =MapUtils.getString(map1,"EndTime");

                }else {
                    TuiDay = MapUtils.getString(map1,"Owerdealupdtm");
                }



                double HetongSumMoney = 0;


            map1.put("Relation",map1.get("OwDeId"));
                List<Map<String,Object>> OwerMoneyList = zpcGmioFeign.getOwerdeal(map1);
                for (Map<String,Object> zczsaa:OwerMoneyList){
                    HetongSumMoney = CompuUtils.add(HetongSumMoney,MapUtils.getDouble(zczsaa,"BiMoney"));
                }


                String LastTuiDay = MapUtils.getString(OwerMoneyList.get(OwerMoneyList.size()-1),"LateDay"); //退房款项最后支付日期


                String zczs = MapUtils.getString(OwerMoneyList.get(1),"StartTime");

                String zhangdanday =zczs.substring(zczs.length()-2,zczs.length());//账单日



                double YaMoney = CompuUtils.mul(MapUtils.getDouble(map1,"MonthPrice"),MapUtils.getDouble(map1,"Pair"));



                map1.put("TuiDay",TuiDay);//退租日期
                map1.put("LastTuiDay",LastTuiDay);//退房最后款项支付日期
                map1.put("zhangdanday",zhangdanday);//账单日
                map1.put("YaMoney",YaMoney);//押金
                map1.put("HetongSumMoney",HetongSumMoney);//合同总金额




                map1.put("oneYear",oneYear);
                map1.put("twoYear",twoYear);
                map1.put("threeYear",threeYear);
                map1.put("fourYear",fourYear);
                map1.put("fiveYear",fiveYear);
                map1.put("sixYear",sixYear);
                map1.put("sevenYear",sevenYear);
                map1.put("eightYear",eightYear);




                map1.put("xuzuoneYear",xuzuoneYear);
                map1.put("xuzutwoYear",xuzutwoYear);
                map1.put("xuzuthreeYear",xuzuthreeYear);
                map1.put("xuzufourYear",xuzufourYear);
                map1.put("xuzufiveYear",xuzufiveYear);
                map1.put("xuzusixYear",xuzusixYear);


















            }








            return pageUtils.getJsonResult(getHouOwer,params,cout);
        }





    }

    /**
     * 出租率平均房价
     * @param params
     * @param pager
     * @return
     */
    public JsonResult HouRental(Map<String,Object> params,Pager pager){
        JsonResult result = new JsonResult();


        pager.setPageno((Integer) params.get("pageno"));
        pager.setPagesize((Integer) params.get("pagesize"));
        pager.setPagerNecessary(params,pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout = houOwerMapper.getComMameCount(params);
        if(cout == 0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(),params,cout);
        }else {
            List<Map<String, Object>> getHouOwer = houOwerMapper.getComMameList(params);
            for (Map<String,Object> map1:getHouOwer){


                int chuzuSum = 0;//出租总间数
                 int shishouSum = 0;//实收中间数
                double chuzulv = 0;//出租率
                int yanshouSum = 0;//验收总间数
                double yunyinglv = 0;//运营间数出租率
                double summoney = 0;
                double pingjunmoney = 0;
                int daizusum = 0;
                int zhuangxiusum = 0;

                double roomMoney = 0;
                List<String> HouList = new ArrayList<>();

                List<Map<String,Object>> mapooList = houOwerMapper.getHouCom(map1);
                shishouSum = mapooList.size();
                for (Map<String,Object> mapoo:mapooList){


                    roomMoney = CompuUtils.add(roomMoney,MapUtils.getDouble(mapoo,"UnifiedPricing"));
                   if(MapUtils.getInteger(mapoo,"histate")==2){
                       chuzuSum++;
                       HouList.add(MapUtils.getString(mapoo,"HouInfoId"));
                   }


                   if (MapUtils.getInteger(mapoo,"hstate")==7){
                       yanshouSum++;
                   }

                    if (MapUtils.getInteger(mapoo,"hstate")==7&&MapUtils.getInteger(mapoo,"histate")!=2){
                        daizusum++;
                    }
                    if (MapUtils.getInteger(mapoo,"hstate")==4){
                        zhuangxiusum++;
                    }
                }

                 if (HouList.size()>0) {
                     map1.put("HouList",HouList);
                     List<Map<String,Object>> ceshimap =  houOwerMapper.getPactHou(map1);
                     for (Map<String,Object> one:ceshimap){
                         int a = DateUtils.getMonthDiff(MapUtils.getString(one,"EndTime"),MapUtils.getString(one,"StartTime"),"yyyy-MM");
                         summoney = CompuUtils.add(summoney,CompuUtils.mul(MapUtils.getDouble(one,"PaActMonthPrice"),(double)a));
                     }
                 }


                 if (chuzuSum!=0){
                chuzulv = CompuUtils.div((double) chuzuSum,(double)shishouSum,2);
                yunyinglv = CompuUtils.div((double) chuzuSum,(double)yanshouSum,2);
                 }

                 if (roomMoney!=0) {
                     pingjunmoney = CompuUtils.div((double) roomMoney, (double) shishouSum, 2);
                 }
                map1.put("chuzuSum",chuzuSum);
                map1.put("shishouSum",shishouSum);
                map1.put("chuzulv",chuzulv);
                map1.put("yanshouSum",yanshouSum);
                map1.put("yunyinglv",yunyinglv);
                map1.put("summoney",summoney);
                map1.put("pingjunmoney",pingjunmoney);
                map1.put("zhuangxiusum",zhuangxiusum);
                map1.put("daizusum",daizusum);
                map1.remove("HouList");
            }
            return pageUtils.getJsonResult(getHouOwer,params,cout);
        }
    }

    /**
     * 出租合同基础表
     * @param params
     * @param pager
     * @return
     */
    public JsonResult RentHouList(Map<String,Object> params,Pager pager) {
        JsonResult result = new JsonResult();



        pager.setPageno((Integer) params.get("pageno"));
        pager.setPagesize((Integer) params.get("pagesize"));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();


        List<String> TenIdList = new ArrayList<>();
        if (FieldCheckUtil.isMapFieldNotNull(params ,new String[]{"TenName","TenPhone"})){

            List<Map<String,Object>> tenList = tenantFeign.getTenNameandPhone(params);
            if (tenList.size()>0&&tenList!=null){
                for (Map<String,Object> mapceshia:tenList){
                    TenIdList.add(MapUtils.getString(mapceshia,"TenId"));
                }

            }

        }

        if (TenIdList.size()>0){
            params.put("TenIdList",TenIdList);
        }

        Integer cout = houOwerMapper.getRentHouCount(params);
        if (cout == 0) {
            return pageUtils.getJsonResult(new ArrayList<Map<String, Object>>(), params, cout);
        } else {
            List<Map<String, Object>> getRentHouList = houOwerMapper.getRentHouList(params);

            for (Map<String,Object> map1:getRentHouList) {

                Integer countPaid = houOwerMapper.getPactType(map1);
                if (countPaid==0){
                    map1.put("PaIdType","新签");
                }else{
                    map1.put("PaIdType","续签");
                }
                List<Map<String,Object>> tenListss = tenantFeign.getTenNameandPhone(map1);

                map1.put("TenName",tenListss.get(0).get("Realname"));
                map1.put("TenPhone",tenListss.get(0).get("TenPhone"));

                List<Map<String, Object>> shardList = houOwerMapper.getPactShard(map1);
                map1.put("shardList",shardList);

                Map<String,Object> map0ne =  zpcGmioFeign.getcharge(map1);
                map1.putAll(map0ne);

                Map<String,Object> mapWat =  zpcGmioFeign.getWatNumber(map1);
                if (mapWat !=null){
                    map1.put("WatThisMonthRead",mapWat.get("ThisMonthRead"));
                }

                Map<String,Object> mapGas =  zpcGmioFeign.getGasNumber(map1);

                if (mapWat !=null){
                    map1.put("GasThisMonthRead",mapGas.get("ThisMonthRead"));
                }

                Map<String,Object> ceshiMap = new HashMap<>();
                ceshiMap.put("HouId",map1.get("HouId"));
                ceshiMap.put("PaIdinstm",map1.get("PaIdinstm"));

                String HouInfoId = MapUtils.getString(map1,"HouInfoId");
                String HouInfoIdS = HouInfoId.replace(HouInfoId.charAt(HouInfoId.length()-1)+"","Z");

                ceshiMap.put("HouInfoId",HouInfoIdS);
                Map<String,Object> mapEle =  zpcGmioFeign.getEleNumber(ceshiMap);
                if (mapEle !=null){
                    map1.putAll(mapEle);
                }

                ceshiMap.put("HouInfoId",map1.get("HouInfoId"));
                Map<String,Object> mapEles =  zpcGmioFeign.getEleNumber(ceshiMap);

                if (mapEles !=null){
                    map1.put("EleThisMonthSum",mapEles.get("ThisMonthEleSum"));
                }


                //服务中心
                String deptId = MapUtils.getString(map1,"MiId");
                Map<String,Object> fuwuMap =  zzuulFeign.getDeptInfoById(deptId);
                map1.putAll(fuwuMap);



                Map<String,Object> ceshizulin = new HashMap<>();

                //租赁顾问
                ceshizulin.put("UserId",map1.get("SysUserId"));
                JsonResult zuulResult = zuulFeign.getUserMap(ceshizulin);
                Map<String,Object> zuulMap = (Map<String, Object>) zuulResult.get("result");
                if (zuulMap!=null) {
                    map1.put("ZuLinName", zuulMap.get("UserName"));
                }
                //管家

                ceshizulin.put("UserId",map1.get("pSysUserId"));
                JsonResult zuulResults = zuulFeign.getUserMap(ceshizulin);
                Map<String,Object> zuulMaps = (Map<String, Object>) zuulResults.get("result");
                if (zuulMaps!=null) {
                    map1.put("GuanJiaName", zuulMaps.get("UserName"));
                }

                map1.put("area_id",map1.get("CorId"));

                //所属公司
               Map<String,Object> mapArea = zuulFeign.getArea(map1);
                System.out.println("11111111111111111111111111");
                System.out.println("mapArea"+mapArea);
               map1.putAll(mapArea);


            }
            return pageUtils.getJsonResult(getRentHouList, params, cout);
        }
    }

    /**
     * 新签平均房价/空值天数统计表
     * @param params
     * @param pager
     * @return
     */
    public JsonResult getPactqianList(Map<String,Object> params,Pager pager) {
        JsonResult result = new JsonResult();
        pager.setPageno((Integer) params.get("pageno"));
        pager.setPagesize((Integer) params.get("pagesize"));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();

        Integer cout = houOwerMapper.getPactqianCount(params);
        if (cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String, Object>>(), params, cout);
        }else{

            List<Map<String, Object>> getPactqianList = houOwerMapper.getPactqianList(params);

            for (Map<String,Object> map1:getPactqianList){


                Integer getPactTypeInt=  houOwerMapper.getPactType(map1);
                if (getPactTypeInt==0){
                    map1.put("PaidType","新签");
                }else{
                    map1.put("PaidType","续签");
                }


                //服务中心
                String deptId = MapUtils.getString(map1,"MiId");
                Map<String,Object> fuwuMap =  zzuulFeign.getDeptInfoById(deptId);
                map1.putAll(fuwuMap);


                //月数
                int PaIdmonth = DateUtils.getMonthDiff(MapUtils.getString(map1,"EndTime"),MapUtils.getString(map1,"StartTime"),"yyyy-MM");
                map1.put("PaIdmonth",PaIdmonth);

                //总金额
                map1.put("PaIdSumMoney",CompuUtils.mul(MapUtils.getDouble(map1,"PaActMonthPrice"),PaIdmonth));

                //租赁顾问
                Map<String,Object> ceshizulin = new HashMap<>();
                ceshizulin.put("UserId",map1.get("SysUserId"));
                JsonResult zuulResult = zuulFeign.getUserMap(ceshizulin);
                Map<String,Object> zuulMap = (Map<String, Object>) zuulResult.get("result");
                if (zuulMap!=null) {
                    map1.put("ZuLinName", zuulMap.get("UserName"));
                }



                String TuOrYanMonth ="";
                int Kongzhi=0;

              Integer PaIdStatus = MapUtils.getInteger(map1,"Status");
                if (PaIdStatus==0){//表示在租中


                    if (MapUtils.getString(map1,"updterm").equals("RdHousingService.updateHoHousingImg")){
                        TuOrYanMonth = MapUtils.getString(map1,"yanfangupdtm");
                        Kongzhi = Math.abs(DateUtils.betweenDay(MapUtils.getString(map1,"StartTime"),MapUtils.getString(map1,"yanfangupdtm")));
                    }



                }else if(PaIdStatus==1){//表示已经退租

                    Map<String,Object> ceshi = zpcGmioFeign.getTenwithdrawal(map1);
                    if (ceshi!=null){
                        TuOrYanMonth = MapUtils.getString(ceshi,"CheckOutTime");

                        Kongzhi = Math.abs(DateUtils.betweenDay(MapUtils.getString(map1,"CheckOutTime"),MapUtils.getString(map1,"StartTime")));
                    }



                }

            map1.put("TuOrYanMonth",TuOrYanMonth);
            map1.put("Kongzhi",Kongzhi);




            }
            return pageUtils.getJsonResult(getPactqianList, params, cout);
        }

    }



}
